
package com.ukao.steward.pesenter.valetRunners;

import com.ukao.steward.base.BasePresenter;
import com.ukao.steward.bean.OrderDetailBean;
import com.ukao.steward.bean.OrderInfoBean;
import com.ukao.steward.bean.ProductListBean;
import com.ukao.steward.bean.ReceiptBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.bean.TradeOrderBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.L;
import com.ukao.steward.util.T;
import com.ukao.steward.view.valetRunners.OrderdetailView;

import java.util.HashMap;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class OrderdetailPesenter extends BasePresenter<OrderdetailView> {
    public OrderdetailPesenter(OrderdetailView view) {
        attachView(view);
    }

    /**
     * 获取订单信息
     * @param orderId
     */
    public void orderDetail(String orderId) {
        if (mvpView==null)return;
       mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id", orderId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.orderDetail(Constant.createParameter(mHashMap)),
                new ApiCallback<OrderDetailBean>() {
                    @Override
                    public void onSuccess(OrderDetailBean model) {

                        if (model.getHttpCode() == 200) {
                            OrderDetailBean mOrderDetailBean = model.getData();
                            mvpView.loadOrderDetail(mOrderDetailBean);
                        } else {
                            T.show(model.getMsg());

                        }

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();

                    }
                });

    }


    /**
     查询订单交易列表
     */
    public void tradeOrder( String  orderId  ) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.tradeOrder(Constant.createParameter(mHashMap)),
                new ApiCallback<TradeOrderBean>() {
                    @Override
                    public void onSuccess(TradeOrderBean model) {

                        if (model.getHttpCode() == 200) {
                            if (!CheckUtils.isEmpty(model.getData().getList())){
                                mvpView.loadTradeOrder(model.getData().getList());

                            }

                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }
    /**
     查询入库衣物列表
     */
    public void orderProductList( String  orderId   ) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.orderProductList(Constant.createParameter(mHashMap)),
                new ApiCallback<ProductListBean>() {
                    @Override
                    public void onSuccess(ProductListBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.loadProductList(model.getData());

                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }


    /**
     * 支付订单详情
     *
     * @param id
     */
    public void payInfo(String  id ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id",id);
        mHashMap.put("accessToken", SaveParams.getToken());
        L.i("mHashMap="+mHashMap.toString());
        mvpView.showLoading();
        addSubscription( apiStores.payInfo(Constant.createParameter(mHashMap)),
                new ApiCallback<OrderInfoBean>() {
                    @Override
                    public void onSuccess(OrderInfoBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.OrderInfoSucceed(model.getData());


                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }

    /**
     * 支付解锁
     * @param orderId
     */
    public void unlockOrder(final  String orderId    ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.unlockOrder(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (model.getHttpCode() == 200) {

                            payInfo(orderId);

                        } else {

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                    }

                });
    }


    /**
     *打印订单
     */
    public void printReceipt( String  id ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",id);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.printReceipt(Constant.createParameter(mHashMap)),
                new ApiCallback<ReceiptBean>() {
                    @Override
                    public void onSuccess(ReceiptBean model) {
                        if (model.getHttpCode()==200){
                            mvpView.loadReceipt(model.getData());
                        }else{
                            mvpView.showError(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }


    /**
     取件员  签收
     */
    public void signParcel(String    orderId) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.signParcel(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.receivingSucceed(model.getMsg());
                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }





}
